package com.learn.controller;

import com.learn.mapper.LagouUserMapper;
import com.learn.service.AuthCodeService;
import com.learn.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @program: learn-parent
 * @description
 * @author: luffy
 * @create: 2020-09-16 15:25
 */
@RestController
@RequestMapping("/user")
@Slf4j
public class UserController {

    @Reference
    private AuthCodeService authCodeService;
    @Autowired
    private UserService userService;
    @Autowired
    private LagouUserMapper lagouUserMapper;

    @RequestMapping("/createCode/{email}")
    public boolean getUser(@PathVariable String email) {
        log.info("进入了createCode方法");
        return authCodeService.createAuthCode(email);
    }

    @GetMapping("/registry/{email}/{password}/{code}")
    public boolean register(@PathVariable String email, @PathVariable String password, @PathVariable String code, HttpServletResponse response) {
        log.info("进入注册方法");
        return userService.registry(email,password,code,response);
    }

    @RequestMapping("/welcome/{email}/{password}")
    public String getUserInfo(@PathVariable String email, @PathVariable String password, HttpServletResponse response,HttpServletRequest request) {
        log.info(request.getCookies().toString());
        String login = userService.login(email, password, response);
        if (login.equals("密码错误")) {
            return "密码错误";
        } else if (login.equals("邮箱不存在")) {
            return "邮箱不存在";
        }else{
            return login;
        }
    }


    @RequestMapping("/index/{email}")
    public String index(@PathVariable String email) {
        return lagouUserMapper.findLagouUser(email).get(0).getEmail();
    }
}
